package pl.umk.mat.fred.unicore.urc.storagemanagement.actions.findNavigation;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import org.eclipse.jface.action.Action;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import pl.umk.mat.fred.unicore.urc.storagemanagement.actions.FindAction.SearchResult;
import pl.umk.mat.fred.unicore.urc.storagemanagement.views.StorageBrowserView;

public class NextSearchResultAction extends Action {

	private StorageBrowserView view;
	private ListIterator<SearchResult> itr;
	private List<String> listOfMulticatalogs;
	private Direction lastDirection;

	public NextSearchResultAction(StorageBrowserView view) {
	  super();
	  this.view = view;
	  this.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));
  }
	
	public enum Direction {FORWARD, BACKWARD};
	
	public void run() {
		System.out.println("Next ");
		if (Direction.BACKWARD.equals(lastDirection) && itr.hasNext()) {
			itr.next();
		}
		if (itr != null) {
			if (itr.hasNext()) {
				SearchResult result = itr.next();
				CommonTools.trySelectTreeNode(this, view, listOfMulticatalogs, result);
				if (!itr.hasNext()) {
					this.disable();
				}
			} else {
				this.disable();
				view.getPrevSearchResultAction().enable();
			}
			if (itr.hasPrevious()) {
				view.getPrevSearchResultAction().enable();
			} else {
				view.getPrevSearchResultAction().disable();
			}
		}
		lastDirection = Direction.FORWARD;
	}
	
	public void setIterator(ListIterator<SearchResult> itr) {
		this.itr = itr;
  }

	public void enable() {
	  this.setEnabled(true);
	  this.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));
  }

	public void disable() {
	  this.setEnabled(false);
	  this.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD_DISABLED));
  }

	public List<String> getListOfMulticatalogs() {
  	return listOfMulticatalogs;
  }

	public void clearListOfMulticatalogs() {
  	this.listOfMulticatalogs = new ArrayList<String>();
  }

	public Direction getLastDirection() {
  	return lastDirection;
  }

	public void setLastDirection(Direction lastDirection) {
  	this.lastDirection = lastDirection;
  }
	
}
